package com.woniuxy.Session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @auther linke
 * @create 2021-10-22 10:42
 * Session 会话
 */
@WebServlet("/sessionDemo02")
public class SessionDemo02 extends HttpServlet {

    /**
     * 获取session 的值
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("sessionDemo02...执行了");
        //获取session对象
        HttpSession session = req.getSession();
        //获取session对象的key
        Object value = session.getAttribute("name");

        String cookie = req.getHeader("cookie");

        System.out.println(cookie);//name=%E5%BC%A0%E4%B8%89; JSESSIONID=C992C5FEC5A3D6CB366686DBC1543C27; Idea-ccd6a49b=669dffcf-1a86-4adc-9eab-1e64a6c2c36e
        System.out.println(value);//tom
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
